#include "stdafx.h"
#include "Window.h"
#include "resource.h"
#include <assert.h>
#include "Director.h"

#define MAX_LOADSTRING 100

#define WM_SOCKET WM_USER+0 

Window::Window(std::string &name)
	: _winName(name)
	, _iCmdShow(SW_SHOWDEFAULT)
{
	MyRegisterClass();
	InitInstance();
	ShowWindow(_hWnd, _iCmdShow);
	UpdateWindow(_hWnd);
}

Window::~Window()
{
	DestroyWindow(_hWnd);
}

LRESULT CALLBACK Window::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;

	HMENU menu = nullptr;

	switch (message)
	{
	case WM_CREATE:
	{
		menu = LoadMenu(GetModuleHandle(0), MAKEINTRESOURCE(IDR_MENU3));
		menu = GetSubMenu(menu, 0);
	}
	break;
	case WM_COMMAND:
		wmId = LOWORD(wParam);
		wmEvent = HIWORD(wParam);
		// Parse the menu selections:
		//switch (wmId)
		//{
		////case IDM_ABOUT:
		//	//DialogBox(g_hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
		////	break;
		////case IDM_EXIT:
		////	DestroyWindow(hWnd);
		////	break;
		//default:
		//	return DefWindowProc(hWnd, message, wParam, lParam);
		//}
		break;
	case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps);
		// TODO: Add any drawing code here...

		EndPaint(hWnd, &ps);
		break;
	case WM_RBUTTONDOWN:
	{
		POINT pt;
		pt.x = LOWORD(lParam);
		pt.y = HIWORD(lParam);
		ClientToScreen(hWnd, &pt);
		TrackPopupMenu(menu, TPM_RIGHTBUTTON, pt.x, pt.y, 0, hWnd, nullptr);
	}
	break;
	case WM_SOCKET:
	{
		if (WSAGETSELECTERROR(lParam))
		{
			closesocket(wParam);
			break;
		}
		switch (WSAGETSELECTEVENT(lParam))
		{
		case FD_CONNECT:
		{
		}
		break;
		case FD_ACCEPT:
		{
			SOCKET connfd = accept(wParam, nullptr, nullptr);
			if (connfd == INVALID_SOCKET)
			{
				wsa_error_handling("accept() error");
				break;
			}
			OutputDebugString("nn");
			// Associate client socket with FD_READ and FD_CLOSE event  
			WSAAsyncSelect(connfd, hWnd, WM_SOCKET, FD_READ | FD_WRITE | FD_OOB | FD_CLOSE);

			Director::getInstance()->getProtocol()->push(connfd);
		}
		break;
		case FD_READ:
		{
			Director::getInstance()->getProtocol()->RecvMsg(wParam);
		}
		break;
		case FD_OOB:
		{

		}
		break;
		case FD_WRITE:
		{
		}
		break;
		case FD_CLOSE:
		{

		}
		break;
		default:
			break;
		}
	}
	break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}

ATOM Window::MyRegisterClass()
{
	HINSTANCE hInst = GetModuleHandle(0);

	char className[32] = { 0 };
	size_t size = sizeof(className);
	LoadString(hInst, IDS_STRING105, className, size);

	WNDCLASSEX wcex;
	wcex.cbSize = sizeof(WNDCLASSEX);
	wcex.style = CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc = WndProc;
	wcex.cbClsExtra = 0;
	wcex.cbWndExtra = 0;
	wcex.hInstance = hInst;
	wcex.hIcon = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_ICON1));
	wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
	wcex.lpszMenuName = MAKEINTRESOURCE(IDR_MENU2);
	wcex.lpszClassName = className;
	wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_ICON2));

	return RegisterClassEx(&wcex);
}

void Window::InitInstance()
{

	HINSTANCE hInst = GetModuleHandle(0);

	char className[32] = { 0 };
	size_t size = sizeof(className);
	LoadString(hInst, IDS_STRING105, className, size);

	_hWnd = CreateWindow(className, _winName.c_str(), WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInst, NULL);

	if (!_hWnd)
	{
		MessageBox(nullptr, "create window don't success!", "message", MB_OK);
		OutputDebugString("create window don't success");
		return;
	}
	OutputDebugString("create window success!");
}

